/**
 * 
 */
package com.buss.core.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.buss.core.exception.ServiceException;
import com.buss.core.model.Organization;
import com.buss.core.model.User;
import com.buss.core.persistence.Pagination;
import com.buss.core.persistence.ResultList;
import com.buss.core.service.OrganizationService;
import com.buss.core.service.UserService;
import com.buss.core.sysparam.SysStaticParam;
import com.buss.core.utils.StringUtil;
import com.buss.core.validator.ResultMessage;

/**
 * @author 高小勇
 * 创建时间:2011-5-29
 * wcms2.0
 */
@RequestMapping("/admin")
@Controller
public class UserController extends BaseController {

	
	@Autowired
	private UserService userService;
	
	@Autowired
	private OrganizationService organizationService;
	
	@Override
	/**
	 * 用户管理页面
	 */
	@RequestMapping("/user/page")
	public ModelAndView listPage(@RequestParam(required=false) String menuid) {
		ModelAndView view = new ModelAndView("user/listpage");
		view.addObject(SysStaticParam._MENUID_NAME, menuid);
		return view;
	}

	/**
	 * 修改用户密码页面
	 * @param userId
	 * @return
	 */
	@RequestMapping("/user/editUserPassPage/{userId}")
	public ModelAndView editPasswordPage(@PathVariable String userId){
		ModelAndView view = new ModelAndView("user/edituserpass");
		view.addObject("userId", userId);
		return view;
	}
	
	@RequestMapping("/user/selectUserPage")
	public ModelAndView selectUserPage(){
		ModelAndView view = new ModelAndView("user/selectuser");
		return view;
	}
	
	@RequestMapping("/user/{type}")
	public ModelAndView editUserPage(@PathVariable String type,@RequestParam(required = false) Long userId,
			@RequestParam(required = false) Long organId){
		ModelAndView view = new ModelAndView("user/edit");
		User user = new User();
		if("editpage".equals(type)){
			user = userService.loadUser(userId);			
		}else{
			if(organId != null){
				Organization organ = organizationService.loadOrganization(organId);
				user.setOrgan(organ);
				user.setIsSalesMan(false);
			}
		}
		view.addObject("user", user);
		return view;
	}
	
	/**
	 * 新增用户信息
	 * @param user
	 * @return
	 */
	@RequestMapping("/user/add")
	@ResponseBody
	public Map<String,Object> add( User user,HttpServletRequest request){
		try{
			user.setCompanyId(getCompanyId(request));
			userService.saveUser(user);
		}catch(ServiceException ex){
			return SysStaticParam.resultMessage(SysStaticParam._RESULT_ERROR, ex.getMessage());
		}
		return SysStaticParam.resultMessage("新增用户信息成功!");
	}
	
	/**
	 * 修改用户信息
	 * @param user
	 * @return
	 */
	@RequestMapping("/user/edit")
	@ResponseBody
	public Map<String,Object> edit( User user,HttpServletRequest request){
		user.setCompanyId(getCompanyId(request));
		userService.editUser(user);
		return SysStaticParam.resultMessage("修改用户信息成功!");
	}
	
	/**
	 * 修改用户密码
	 * @param userId
	 * @param password
	 * @return
	 */
	@RequestMapping("/user/editpassword/{userId}")
	@ResponseBody
	public Map<String,Object> edit(@PathVariable Long userId,String password){
		Map<String,Object> map = new HashMap<String, Object>();
		ResultMessage rsMess = new ResultMessage("-1","");
		if(userId == null){
			rsMess.setMess("用户编码不能为空!");
			map.put(SysStaticParam._MESSAGE, rsMess);
			return map;
		}
		if(StringUtil.isEmpty(password)){
			rsMess.setMess("密码不能为空!");
			map.put(SysStaticParam._MESSAGE, rsMess);
			return map;
		}
		User user = userService.loadUser(userId);
		if(user == null){
			rsMess.setMess("没有找到要修改密码的用户信息!");
			map.put(SysStaticParam._MESSAGE, rsMess);
			return map;
		}
		
		userService.editUserPass(userId, password);
		rsMess.setCode("0");
		rsMess.setMess("修改密码成功!");
		map.put(SysStaticParam._MESSAGE, rsMess);
		return map;
	}
	
	/**
	 * 部门调动
	 * @param userIds
	 * @param organId
	 * @return
	 */
	@RequestMapping("/user/organTransfer")
	@ResponseBody
	public Map<String,Object> organTransfer(@RequestParam Long[] userIds, @RequestParam Long organId){
		userService.editOrganTransfer(userIds, organId);
		return SysStaticParam.resultMessage("部门调动成功!");
	}
	
	@RequestMapping("/user/editRoleToUser/{userId}")
	@ResponseBody
	public Map<String,Object> editRoleToUser(@PathVariable Long userId,@RequestParam(required=false, value="ids") Long[] roleIds){
		userService.editRoleToUserId(userId, roleIds);
		return SysStaticParam.resultMessage("保存员工角色信息成功!");
	}
	
	/**
	 * 获取用户列表信息
	 * @param organId
	 * @param userName
	 * @param disabled
	 * @param page
	 * @return
	 */
	@RequestMapping("/user/list/{organId}")
	@ResponseBody
	public Map<String,Object> list(@PathVariable Long organId,@RequestParam(required=false) String userName,
			@RequestParam(required = false) Boolean disabled, Pagination page,HttpServletRequest request){
		if (page != null && page.getPageSize() == 0) page.setPageSize(20);
		ResultList<User> result = userService.queryUser(getCompanyId(request), organId, userName, disabled, page);
		Map<String, Object> map = SysStaticParam.resultMessage("获取用户列表信息成功!");
		map.put("result", result);
		return map;
	}
	
	/**
	 * 获取指定角色下的所有用户信息
	 * @param roleId
	 * @param userName
	 * @param disabled
	 * @param page
	 * @return
	 */
	@RequestMapping("user/listByRoleId/{roleId}")
	@ResponseBody
	public Map<String,Object> listByRoleId(@PathVariable Long roleId,@RequestParam(required = false) String userName,
			@RequestParam(required = false)Boolean disabled, Pagination page,HttpServletRequest request){
		if(page.getPageSize() == 0) page.setPageSize(20);
		ResultList<User> result = userService.queryUserByRoleId(getCompanyId(request),roleId, userName, disabled, page);
		Map<String, Object> map = SysStaticParam.resultMessage("获取用户列表信息成功!");
		map.put(SysStaticParam._RESULT, result);		
		return map; 
	}
	
	/**
	 * 删除用户信息
	 * @param ids
	 * @return
	 */
	@RequestMapping("/user/delete")
	@ResponseBody
	public Map<String,Object> delete(@RequestParam Long[] ids){		
		userService.deleteUser(ids);		
		return SysStaticParam.resultMessage("删除用户信息成功!");
	}
	
	/**
	 * 添加用户到指定角色
	 * @param roleId
	 * @param userIds
	 * @return
	 */
	@RequestMapping("/user/updateUsersRole/{roleId}")
	@ResponseBody
	public Map<String,Object> updateUsersRole(@PathVariable Long roleId,@RequestParam(required=true) Long[] userIds){
		userService.updateUsersRole(userIds, roleId);
		return SysStaticParam.resultMessage("添加用户到角色信息中成功!");
	}
	
	@RequestMapping("/user/removeUsersRole/{roleId}")
	@ResponseBody
	public Map<String,Object> removeUsersRole(@PathVariable Long roleId,@RequestParam(required = false) Long[] userIds){
		userService.removeUsersRole(userIds, roleId);
		return SysStaticParam.resultMessage("取消指定角色中的用户信息成功!");
	}
	
	@Override
	public ModelAndView editPage(@PathVariable String type, @RequestParam(required = false) Long id) {		
		return null;
	}


}
